<p>Encrypting data is security-sensitive. It has led in the past to the following vulnerabilities:</p>
<ul>
  <li> <a href="http://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2017-7902">CVE-2017-7902</a> </li>
  <li> <a href="http://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2006-1378">CVE-2006-1378</a> </li>
  <li> <a href="http://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2003-1376">CVE-2003-1376</a> </li>
</ul>
<p>Proper encryption requires both the encryption algorithm and the key to be strong. Obviously the private key needs to remain secret and be renewed
regularly. However these are not the only means to defeat or weaken an encryption.</p>
<p> </p>
<p>This rule flags function calls that initiate encryption/decryption.</p>
<h2>Ask Yourself Whether</h2>
<ul>
  <li> the private key might not be random, strong enough or the same key is reused for a long long time. </li>
  <li> the private key might be compromised. It can happen when it is stored in an unsafe place or when it was transferred in an unsafe manner. </li>
  <li> the key exchange is made without properly authenticating the receiver. </li>
  <li> the encryption algorithm is not strong enough for the level of protection required. Note that encryption algorithms strength decreases as time
  passes. </li>
  <li> the chosen encryption library is deemed unsafe. </li>
  <li> a nonce is used, and the same value is reused multiple times, or the nonce is not random. </li>
  <li> the RSA algorithm is used, and it does not incorporate an Optimal Asymmetric Encryption Padding (OAEP), which might weaken the encryption.
  </li>
  <li> the CBC (Cypher Block Chaining) algorithm is used for encryption, and it's IV (Initialization Vector) is not generated using a secure random
  algorithm, or it is reused. </li>
  <li> the Advanced Encryption Standard (AES) encryption algorithm is used with an unsecure mode. See the recommended practices for more information.
  </li>
</ul>
<p>You are at risk if you answered yes to any of those questions.</p>
<h2>Recommended Secure Coding Practices</h2>
<ul>
  <li> Generate encryption keys using secure random algorithms. </li>
  <li> When generating cryptographic keys (or key pairs), it is important to use a key length that provides enough entropy against brute-force
  attacks. For the Blowfish algorithm the key should be at least 128 bits long, while for the RSA algorithm it should be at least 2048 bits long.
  </li>
  <li> Regenerate the keys regularly. </li>
  <li> Always store the keys in a safe location and transfer them only over safe channels. </li>
  <li> If there is an exchange of cryptographic keys, check first the identity of the receiver. </li>
  <li> Only use strong encryption algorithms. Check regularly that the algorithm is still deemed secure. It is also imperative that they are
  implemented correctly. Use only encryption libraries which are deemed secure. Do not define your own encryption algorithms as they will most
  probably have flaws. </li>
  <li> When a nonce is used, generate it randomly every time. </li>
  <li> When using the RSA algorithm, incorporate an Optimal Asymmetric Encryption Padding (OAEP). </li>
  <li> When CBC is used for encryption, the IV must be random and unpredictable. Otherwise it exposes the encrypted value to crypto-analysis attacks
  like "Chosen-Plaintext Attacks". Thus a secure random algorithm should be used. An IV value should be associated to one and only one encryption
  cycle, because the IV's purpose is to ensure that the same plaintext encrypted twice will yield two different ciphertexts. </li>
  <li> The Advanced Encryption Standard (AES) encryption algorithm can be used with various modes. Galois/Counter Mode (GCM) with no padding should be
  preferred to the following combinations which are not secured:
    <ul>
      <li> Electronic Codebook (ECB) mode: Under a given key, any given plaintext block always gets encrypted to the same ciphertext block. Thus, it
      does not hide data patterns well. In some senses, it doesn't provide serious message confidentiality, and it is not recommended for use in
      cryptographic protocols at all. </li>
      <li> Cipher Block Chaining (CBC) with PKCS#5 padding (or PKCS#7) is susceptible to padding oracle attacks. </li>
    </ul> </li>
</ul>
<h2>Sensitive Code Example</h2>
<pre>
// === javax.crypto ===
import javax.crypto.Cipher;
Cipher c = Cipher.getInstance(...);  // Sensitive

// === apache.commons.crypto ===
import java.util.Properties;
import org.apache.commons.crypto.utils.Utils;
import org.apache.commons.crypto.cipher.CryptoCipherFactory;
import org.apache.commons.crypto.cipher.CryptoCipherFactory.CipherProvider;

Properties properties = new Properties();
properties.setProperty(CryptoCipherFactory.CLASSES_KEY, CipherProvider.OPENSSL.getClassName());
final String transform = "AES/CBC/PKCS5Padding";
Utils.getCipherInstance(transform, properties);  // Sensitive
</pre>
<h2>See</h2>
<ul>
  <li> <a href="https://www.owasp.org/index.php/Top_10-2017_A3-Sensitive_Data_Exposure">OWASP Top 10 2017 Category A3</a> - Sensitive Data Exposure
  </li>
  <li> <a href="https://www.owasp.org/index.php/Top_10-2017_A6-Security_Misconfiguration">OWASP Top 10 2017 Category A6</a> - Security
  Misconfiguration </li>
  <li> <a href="http://cwe.mitre.org/data/definitions/321.html">MITRE, CWE-321</a> - Use of Hard-coded Cryptographic Key </li>
  <li> <a href="http://cwe.mitre.org/data/definitions/322.html">MITRE, CWE-322</a> - Key Exchange without Entity Authentication </li>
  <li> <a href="http://cwe.mitre.org/data/definitions/323.html">MITRE, CWE-323</a> - Reusing a Nonce, Key Pair in Encryption </li>
  <li> <a href="http://cwe.mitre.org/data/definitions/324.html">MITRE, CWE-324</a> - Use of a Key Past its Expiration Date </li>
  <li> <a href="http://cwe.mitre.org/data/definitions/325.html">MITRE, CWE-325</a> - Missing Required Cryptographic Step </li>
  <li> <a href="http://cwe.mitre.org/data/definitions/326.html">MITRE, CWE-326</a> - Inadequate Encryption Strength </li>
  <li> <a href="http://cwe.mitre.org/data/definitions/327.html">MITRE, CWE-327</a> - Use of a Broken or Risky Cryptographic Algorithm </li>
  <li> <a href="https://www.sans.org/top25-software-errors/#cat3">SANS Top 25</a> - Porous Defenses </li>
</ul>
<h2>Deprecated</h2>
<p>This rule is deprecated, and will eventually be removed.</p>

